package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.LqsdhrdjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.LqsdhrdjlbBsInfoVo;
import com.ruoyi.report_lq.domain.LqsdhrdjlbBsInfo;
import com.ruoyi.report_lq.mapper.LqsdhrdjlbBsInfoMapper;
import com.ruoyi.report_lq.service.ILqsdhrdjlbBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青闪点和燃点记录主Service业务层处理
 *
 * @author zyb
 * @date 2024-05-14
 */
@RequiredArgsConstructor
@Service
public class LqsdhrdjlbBsInfoServiceImpl implements ILqsdhrdjlbBsInfoService {

    private final LqsdhrdjlbBsInfoMapper baseMapper;

    /**
     * 查询沥青闪点和燃点记录主
     */
    @Override
    @FIllBasicInfo
    public LqsdhrdjlbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询沥青闪点和燃点记录主列表
     */
    @Override
    public TableDataInfo<LqsdhrdjlbBsInfoVo> queryPageList(LqsdhrdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqsdhrdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<LqsdhrdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青闪点和燃点记录主列表
     */
    @Override
    public List<LqsdhrdjlbBsInfoVo> queryList(LqsdhrdjlbBsInfoBo bo) {
        LambdaQueryWrapper<LqsdhrdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqsdhrdjlbBsInfo> buildQueryWrapper(LqsdhrdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqsdhrdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), LqsdhrdjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqcd()), LqsdhrdjlbBsInfo::getLiqcd, bo.getLiqcd());
        lqw.eq(bo.getShiycs() != null, LqsdhrdjlbBsInfo::getShiycs, bo.getShiycs());
        lqw.eq(bo.getShandpjz() != null, LqsdhrdjlbBsInfo::getShandpjz, bo.getShandpjz());
        lqw.eq(bo.getRandpjz() != null, LqsdhrdjlbBsInfo::getRandpjz, bo.getRandpjz());
        lqw.eq(bo.getXiuzhsd() != null, LqsdhrdjlbBsInfo::getXiuzhsd, bo.getXiuzhsd());
        lqw.eq(bo.getXiuzhrd() != null, LqsdhrdjlbBsInfo::getXiuzhrd, bo.getXiuzhrd());
        return lqw;
    }

    /**
     * 新增沥青闪点和燃点记录主
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(LqsdhrdjlbBsInfoBo bo) {

        LqsdhrdjlbBsInfo add = BeanUtil.toBean(bo, LqsdhrdjlbBsInfo.class);
//        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青闪点和燃点记录主
     */
    @Override
    public Boolean updateByBo(LqsdhrdjlbBsInfoBo bo) {
        LqsdhrdjlbBsInfo update = BeanUtil.toBean(bo, LqsdhrdjlbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqsdhrdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青闪点和燃点记录主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
